﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using System.Net;

namespace Hzexe.Wechat.SharpSDK.Message.JSONData.Support
{
    /// <summary>
    /// 发送请求结果基类
    /// </summary>
    [Serializable]
    public abstract class ResultBase
    {
        //日志对象
        protected static readonly log4net.ILog log = null;
        static ResultBase()
        {
            log = log4net.LogManager.GetLogger(typeof(ResultBase));
        }
        
        /// <summary>
        /// 请求结果状态码
        /// </summary>
        public int errcode { get; set; }
        /// <summary>
        /// 请求状态描述
        /// </summary>
        public string errmsg { get; set; }

       /// <summary>
        /// 根据请求页面的访问结果实例化类型
       /// </summary>
       /// <typeparam name="T">要创建的子类型</typeparam>
       /// <param name="url"></param>
       /// <returns></returns>
        public static T GetResult<T>(string url) where T : ResultBase,new()
        {
            var r = default(T);
            string jsonstring = @"{""errcode"":40013,""errmsg"":""http request error""}";
            WebClient wc = new WebClient();
            wc.Encoding = System.Text.Encoding.UTF8;
            try
            {
                jsonstring = wc.DownloadString(url);
            }
            catch (Exception ex)
            {
                log.Warn("请求远程服务器" + url +"失败",ex);
            }
            finally
            {
                wc.Dispose();
            }

            return Newtonsoft.Json.JsonConvert.DeserializeObject<T>(jsonstring);
        }
        /// <summary>
        /// 根据请求页面的访问结果实例化类型
        /// </summary>
        /// <typeparam name="T">要创建的子类型</typeparam>
        /// <param name="poststring">要post的字符串</param>
        /// <param name="url"></param>
        /// <returns></returns>
        public static T GetResult<T>(string url,string poststring) where T : ResultBase, new()
        {
            string jsonstring = @"{""errcode"":40013,""errmsg"":""http request error""}";
            WebClient wc = new WebClient();
            wc.Encoding = System.Text.Encoding.UTF8;
            try
            {
                jsonstring = wc.UploadString(url, null, poststring);
            }
            catch (Exception ex)
            {
                log.Warn("请求远程服务器" + url + "失败", ex);
            }
            finally
            {
                wc.Dispose();
            }
            return Newtonsoft.Json.JsonConvert.DeserializeObject<T>(jsonstring);
        }

        /// <summary>
        /// 根据请求页面的访问结果实例化类型
        /// </summary>
        /// <typeparam name="T">要创建的子类型</typeparam>
        /// <param name="url"></param>
        /// <returns></returns>
        public static MediaResult GetResult(string url) 
        {
            MediaResult r = new MediaResult(s => new MediaSolution(), "");

            return r;
        }
        //MediaResult
    }
}
